<template>
	<view class="container">

		<view class="custom-form">
			<view class="custom-form-item">
				<view class="custom-form-item__label">门店：</view>
				<view class="dynamic-form-item__desc">
					<!-- <uni-data-select 
					v-model="storeId" :localdata="storeOpt" 
					@change="changeStore">
					</uni-data-select> -->
					<uni-data-checkbox v-model="storeIds" multiple :localdata="storeOpt" @change="changeStore"/>
				</view>
			</view>
			<!-- <view class="custom-form-item">
				<view class="custom-form-item__label">商品名称：</view>
				<view class="dynamic-form-item__desc">
				</view>
			</view> -->
			<view class="custom-form-item">
				<view class="custom-form-item__label">选择时间：</view>
				<view class="custom-form-item__desc">
					<uni-datetime-picker v-model="dataRange" type="daterange" @change="changendDate" />
				</view>
			</view>
		</view>

		<view class="uni-padding-wrap uni-common-mt">
			<uni-segmented-control class="uni-padding-wrap_tag" :current="current" :values="items"
				:style-type="styleType" :active-color="activeColor" @clickItem="onClickTab" />
		</view>

		<uni-list class="custom-list">
			<uni-list-item v-for="item in dataList" :key="item.id" clickable @click="onClick(item)">
				<template v-slot:body>
					<view class="slot-box">
						<view class="item-title" style="font-size: 14px;color: #3b4144;font-weight: 600;">
							<text>{{item.name}}</text>
						</view>
						<view class="custom-item" v-if="item.stock || item.storeOut">
							<text class="item-name">{{feildData[current].num}}：</text>
							<text class="item-value">{{item.stock || item.storeOut}}</text>
						</view>
						<view class="custom-item" v-if="item.createdAt">
							<text class="item-name">{{feildData[current].date}}：</text>
							<text class="item-value">
								{{formatDate(item.createdAt)}}
							</text>
						</view>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
		<view class="no-data" v-if="showNoDataTip">
			<text>数据记录为空</text>
		</view>
		<view class="no-data" v-if="isLoading">
			<text>{{loadingText}}</text>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import {
		formatDate
	} from '@/common/util.js';
	export default {
		components: {},
		data() {
			return {
				items: ['入库记录', '出库记录'],
				current: 0,
				activeColor: '#007aff',
				styleType: 'text',
				dataList: [],
				toAui: {
					1: "../../form/pray/pray",
					4: "../../form/feeAsk/feeAsk",
				},
				billtype: 0,
				checkstatus: 0,
				dataRange: [],
				beginDate: '',
				endDate: '',
				isLoading: false,
				loadingText: '加载中...',
				isNoData: false,
				pageCurrent: 0,
				showNoDataTip: false,
				feildData: {
					0: {
						num: "入库数量",
						date: "入库时间"
					},
					1: {
						num: "出库数量",
						date: "出库时间"
					}
				},
				pageNum: 5,
				storeOpt: [],
				storeIds: [],
				storeId: 0,
			};
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				hasLogin: 'user/hasLogin'
			}),
			appConfig() {
				return getApp().globalData.config
			}
		},

		onLoad: function(event) {
			this.queryStoreList();
			if (this.beginDate === '' && this.endDate === '') {
				this.beginDate = formatDate(new Date());
				this.endDate = formatDate(new Date());

				this.dataRange[0] = this.beginDate;
				this.dataRange[1] = this.endDate;
			}
			// 查询列表
			this.queryFormList();
		},

		// 上拉加载
		onReachBottom: function() {
			this.queryFormList();
		},

		methods: {
			queryFormList() {
				if (this.isLoading || this.isNoData) {
					return;
				}
				this.isLoading = true;
				this.isNoData = false;

				const result = new Date(this.endDate);
				result.setDate(result.getDate() + 1);
				this.endDate = formatDate(result);

				let params = {
					"beginDate": this.beginDate,
					"endDate": this.endDate,
					"pageCurrent": this.pageCurrent,
					"pageNum": this.pageNum,
					"goodsName": this.searchName,
					"storeIds": this.storeIds,
				};

				if (this.current === 0) {
					this.$api.goodsInSearch(params).then(result => {
						this.updateDatalist(result);
					}).catch(res => {
						// 失败进行的操作
					})
				} else {
					this.$api.goodsOutSearch(params).then(result => {
						this.updateDatalist(result);
					}).catch(res => {
						// 失败进行的操作
					})
				}
			},

			queryStoreList() {
				let authInfos = this.userInfo.authInfos;
				if (authInfos) {
					let storeIdArr = [];
					Object.entries(this.userInfo.authInfos).forEach(([key, value]) => {
						for (let i = 0; i < value.length; i++) {
							if (storeIdArr.includes(value[i].storeId)) {
								continue;
							} else {
								this.storeOpt.push({
									value: value[i].storeId,
									text: value[i].storeName,
								});
								storeIdArr.push(value[i].storeId);
							}
						}
					});
					
					this.storeIds = storeIdArr;
				} else {
					// 从数据库查询
				}
			},

			updateDatalist(result) {
				if (!result || !Array.isArray(result)) {
					this.isNoData = true;
					this.isLoading = false;
					if (this.dataList.length == 0) {
						this.showNoDataTip = true;
					}
					return;
				}
				this.showNoDataTip = false;
				this.isNoData = (result.length < this.pageNum || result.length > this.pageNum);
				this.dataList = this.dataList.concat(result);
				this.showNoDataTip = this.dataList.length === 0 ? true : false;
				this.isLoading = false;
				if (!this.isNoData) {
					this.pageCurrent = this.pageCurrent + this.pageNum;
				}
			},

			onClickTab(e) {
				if (this.current === e.currentIndex) {
					return;
				}
				this.current = e.currentIndex
				this.initLoadMoreData();
				this.queryFormList();
			},

			onClick(item) {
				console.info(item);
				// 获取当前选中的单号ID
				const billId = item.billid;
				let urlLink = '';
				if (item.billtype == "20") {
					// 请购单
					urlLink = "../../form/pray/pray";
				} else if (item.billtype == "FPAY") {
					// 费用报销
					urlLink = "../../form/feeAsk/feeAsk";
				} else if (item.billtype == "PETT") {
					// 周转借款
					urlLink = "../../form/petty-cash/petty-cash";
				} else if (item.billtype == "SUPP") {
					urlLink = "../../form/vendor-pay/vendor-pay";
				}

				// 获取需要跳转的url
				uni.navigateTo({
					url: urlLink + "?billId=" + billId + "&operation=approveL&checkstatus=" + this.checkstatus,
				});
			},

			initLoadMoreData() {
				this.showNoDataTip = false;
				this.isLoading = false;
				this.isNoData = false;
				this.pageCurrent = 0;
				this.dataList = [];
			},

			changendDate(e) {
				console.log('change事件:', e);
				if (!e || e.length <= 1) {
					return;
				}
				this.beginDate = e[0];
				this.endDate = e[1];
				this.initLoadMoreData();
				this.queryFormList();
			},


			changeStore(e) {
				console.log('changeStore事件:', e);
				if (!e || !e.detail || !e.detail.value) {
					return;
				}
				this.storeIds = e.detail.value;
				debugger;
				this.initLoadMoreData();
				this.queryFormList();
			},
			
			formatDate(timeStr){
				 const date = new Date(timeStr);
				  const year = date.getFullYear();
				  const month = String(date.getMonth() + 1).padStart(2, '0');
				  const day = String(date.getDate()).padStart(2, '0');
				  const hours = String(date.getHours()).padStart(2, '0');
				  const minutes = String(date.getMinutes()).padStart(2, '0');
				  const seconds = String(date.getSeconds()).padStart(2, '0');
				  
				  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			}
		},
	};
</script>

<style lang="scss">
	body {
		background-color: #F0F4F7;
	}
</style>

<style lang="scss" scoped>
	body {
		background-color: #F0F4F7;
	}

	.example-body {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-start;
		align-items: flex-end;
		flex-wrap: wrap;
	}

	.tag-view {
		margin-right: 10px;
	}

	.uni-common-mt {
		margin-top: 0px;
	}

	.uni-padding-wrap {
		// width: 750rpx;
		padding: 0px 0px;
	}

	.content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		// height: 200px;
		text-align: center;
	}

	.content-text {
		font-size: 14px;
		color: #666;
	}

	.color-tag {
		width: 25px;
		height: 25px;
	}

	.uni-list {
		flex: 1;
		background-color: #F0F4F7 !important;
	}

	.uni-list-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		flex-direction: row;
		background-color: #FFFFFF;
	}


	.uni-list-item__container {
		padding: 12px 20px;
		width: 100%;
		flex: 1;
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		border-bottom-style: solid;
		border-bottom-width: 1px;
		border-bottom-color: #eee;
	}

	.uni-list-item__content-title {
		font-size: 14px;
	}

	.uni-tag--circle.data-v-2016016e {
		font-size: 14px;
	}

	.slot-box {
		/* #ifndef APP-NVUE */
		display: inline;
		/* #endif */
		flex-direction: row;
		align-items: center;
		width: 100%;
	}

	.item-title {
		font-size: 16px;
		color: #3b4144;
		font-weight: 600;
		margin-bottom: 10px !important;
	}

	.item-name {
		font-size: 14px;
		color: #3b4144;
	}

	.item-value {
		color: #999;
		font-size: 12px;
	}

	.uni-list-item__content-title {
		font-size: 16px;
	}

	.slot-box {
		view {
			margin-bottom: 5px;
		}
	}

	.tag-view {
		display: inline-flex;
		margin-left: 10px;
	}

	.custom-form {
		background-color: #FFFFFF;
		margin-bottom: 8px;
		background-color: #FFFFFF;
		margin: 8px 10px;
		padding: 6px 0;

		.custom-form-item:last-child {
			border-bottom: 0px solid #f0f0f0;
		}

	}

	.custom-form-item {
		position: relative;
		display: flex;
		flex-direction: row;
		align-items: center;
		font-weight: normal;
		border-bottom: 0px solid #f0f0f0;
		margin: 2px 5px;

		&__label {
			width: 70px;
			font: 14px 'microsoft yahei';
			font-weight: 400;
			line-height: 25px;
			margin: 5px 10px;
		}


		&__desc {
			display: flex;
			flex-direction: column;
			flex: 1;
			font: 14px 'microsoft yahei';
			color: #666666;
			font: 14px 'microsoft yahei';
			margin-right: 10px;
		}

		/deep/.uni-date {
			&__x-input {
				line-height: 30px;
				height: 30px;
			}
		}

		/deep/.uni-date-editor--x .uni-date__icon-clear {
			border: 6px solid transparent;
		}
	}

	.custom-list {
		/deep/.uni-list {
			margin: 0px 10px;
			width: auto;
		}
	}

	.no-data {
		background-color: #FFFFFF;
		margin: 0px 10px;
		text-align: center;
		height: 35px;
		line-height: 35px;
		font-size: 28rpx;
	}

	.loading-more {
		margin: 10px 10px;
		height: 30px;
		background: #FFFFFF;
		text-align: center;
		line-height: 30px;
	}
</style>